﻿using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.ObjectMapping;
using CustomsDocumentOA.Project.CusDocument.Dto;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace CustomsDocumentOA.Project.CusDocument
{
    public class CDOA_ShareRecordsAppService : AsyncCrudAppService<CDOA_ShareRecords, ShareRecordsDto, long, PagedResultRequestDto, UpdateShareRecordsDto, UpdateShareRecordsDto>
    {
        private readonly IObjectMapper _objectMapper;
        private readonly ICustomerAppservice _customerAppservice;
        public CDOA_ShareRecordsAppService(IRepository<CDOA_ShareRecords, long> repository, IObjectMapper objectMapper, ICustomerAppservice customerAppservice) : base(repository)
        {
            _objectMapper = objectMapper;
            _customerAppservice = customerAppservice;
        }

        #region 密码验证登录
        /// <summary>
        /// 密码验证登录
        /// </summary>
        /// <param name="password">密码</param>
        /// <param name="recordId">记录id</param>
        /// <returns></returns>
        public async Task<string> ShareRecordLogin(string password, long recordId)
        {
            var lists = await Repository.FirstOrDefaultAsync(it => it.Id == recordId);//根据id查询数据
            DateTime time = DateTime.Now;
            Random rnd = new Random();
            string str = @"0123456789";
            string strReturn = "";
            for (int i = 0; i < 6; i++)
            {
                strReturn += str.Substring(rnd.Next(0, 9), 1);
            }
            if (lists != null)//如果有数据
            {
                if (lists.cSharePassWord == password)//如果密码正确
                {
                    if (time.AddMinutes(lists.cExpirationDay) <= lists.cExpirationTime)//当前时间+设定时间<=过期时间
                    {
                        return "登陆成功！";
                    }
                    else
                    {
                        //更新密码
                        lists.cSharePassWord = strReturn;
                        await Repository.InsertOrUpdateAsync(lists);
                        return "分享超时";
                    }
                }
                else
                {
                    return "密码不正确";
                }
            }
            else
            {
                return "没有记录";
            }
        }
        #endregion

        #region 创建分享记录
        /// <summary>
        /// 创建分享记录
        /// </summary>
        /// <param name="input">创建分享记录DTO</param>
        /// <returns></returns>
        public async Task<ShareRecordsDto> CreateShareRecord(ShareRecords input)
        {
            Random rnd = new Random();
            string str = @"0123456789";
            string strReturn = "";
            for (int i = 0; i < 6; i++)
            {
                strReturn += str.Substring(rnd.Next(0, 9), 1);
            }
            UpdateShareRecordsDto dto = new UpdateShareRecordsDto();
            dto.cCustomerID = input.cCustomerID;
            dto.cExpirationDay = input.cExpirationDay;
            dto.cExpirationTime = input.cExpirationTime;
            dto.cSharePassWord = strReturn;
            dto.cUserName = input.cUserName;
            var result = await base.CreateAsync(dto);
            return result;
        }
        #endregion

        #region 批量删除
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        #region 折叠代码
        //public async Task DeleteList(List<long> id)
        //{
        // await Repository.DeleteAsync(it => id.Contains(it.Id));
        //}
        #endregion

        #region 折叠代码
        //public async Task DeleteList(List<long> id)
        //{
        //    for (int i = 0; i < id.Count; i++)
        //    {
        //        await Repository.DeleteAsync(it => it.Id == id[i]);
        //    }
        //}
        #endregion
        public async Task DeleteList(string id)
        {
            string[] obj = id.Split(",");
            for (int i = 0; i < obj.Length; i++)
            {
                await Repository.DeleteAsync(long.Parse(obj[i]));
            }
        }
        #endregion
    }
}
